package com.stylefeng.guns.modular.system.api;

import com.alipay.api.internal.util.StringUtils;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.response.JsonResult;
import com.stylefeng.guns.core.response.ResultCode;
import com.stylefeng.guns.core.response.ResultGenerator;
import com.stylefeng.guns.modular.system.model.JarPackage;
import com.stylefeng.guns.modular.system.service.IJarPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping
public class JarPackageApiController extends BaseController {

    @Autowired
    IJarPackageService jarPackageService;

    /**
     * 根据包名查询
     */
    @RequestMapping(value = "/getJarPackage/{jarName}")
    public JsonResult getJarPackage(@PathVariable String jarName){
        JarPackage jp = null;
        if(!StringUtils.isEmpty(jarName)) {
            jp = jarPackageService.selByJarName(jarName);
        }
        return ResultGenerator.genSuccessResult(jp);
    }

    /**
     * 查询所有的jar包
     * */
    @RequestMapping(value = "/jarPackageList")
    public JsonResult list(){
        return ResultGenerator.genSuccessResult(jarPackageService.selectList(null));
    }

    /**
     * 获取是否升级jar包接口
     * */
    @RequestMapping(value = "/upJarPackage/{jarName}/{jarVersion}")
    public JsonResult upJarPackage(@PathVariable String jarName,@PathVariable Integer jarVersion){
        List<JarPackage> jarPackage = jarPackageService.upJarPackage(jarName,jarVersion);
        if(jarPackage==null){
            ResultGenerator.genFailResult("wrong", ResultCode.CHANNEL_NOT_FOUND);
        }
        return ResultGenerator.genSuccessResult(jarPackage);
    }
}
